package com.example.dao.impl;

import com.example.dao.RoleDao;
import com.example.domain.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class RoleDaoImpl implements RoleDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private static RoleDao roleDao;

    public List<Role> getAll() {
        List<Role> roleList = jdbcTemplate
                .query("select * from sys_role", new BeanPropertyRowMapper<Role>(Role.class));
        return roleList;
    }

    public void save(Role role) {
        jdbcTemplate.update("insert into sys_role values (?,?,?)",null,role.getRoleName(),role.getRoleDesc());
    }

    public List<Role> getRolesByUserId(Long userId) {
        List<Role> roles = jdbcTemplate.query("select * from sys_role r,sys_user_role ur where r.id=ur.roleId and ur.userId=?"
                , new BeanPropertyRowMapper<Role>(Role.class), userId);
        return roles;
    }
}
